package com.murphy.algorithm.demo;

import java.util.Stack;

/**
 * @author dongsufeng
 * @version 4.0
 * @date 2023/10/19 09:59
 */
public class GenTow {

    public static void main(String[] args) {
        GenTow genTow = new GenTow();
        genTow.gen2(4);
    }
    public void gen(int num){
        String result = null;
        for (int i = 1; i < num * 2; i++){
            //如果为空输出@
            if (result == null || "".equals(result.trim())){
                result = "@";
            }else {
                if (i > num) {
                    result = result.substring(0, result.length() - 1);
                } else {
                    if (result.endsWith("@")) {
                        result = result.concat("#");
                    } else {
                        result = result.concat("@");
                    }
                }
            }
            System.out.println(result);
        }
    }
    //
    public String gen2(int num){
        String result = "";
        Stack<String> stack = new Stack<>();
        for (int i = 0;i<=num;i++) {

            if (stack.isEmpty()) {
                stack.push("@");
                result = stack.peek();
                System.out.println(result);
            } else {
                if ("@".equals(stack.peek())) {
                    stack.push("#");
                    result = result.concat(stack.peek());
                } else {
                    stack.push("@");
                    result = result.concat(stack.peek());
                }
                System.out.println(result);
                if (stack.size() == num) {
                    while (!stack.isEmpty()) {
                        result = result.substring(0, result.length() - 1);
                        System.out.println(result);
                    }
                }
            }
        }
        return result;
    }
}
